//
//  FLMClaimOrderController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/15.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMClaimOrderController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "FLMTextFieldCell.h"
#import "FLMManageOrderDetailCell.h"
#import "ZLPhotoActionSheet.h"
@interface FLMClaimOrderController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong) NSString *selectStr, *orderNoStr;
@property (nonatomic,strong) UIButton *imageButton;
@property (nonatomic,strong) NSMutableArray *selectArr;
@property (nonatomic,strong) UIImage *selectImage;

@end

@implementation FLMClaimOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"订单认领";
    self.selectArr = [@[@"淘宝",@"商城订单"]mutableCopy];
    [self layoutUI];
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setTitleColor:black forState:UIControlStateNormal];
    [rightButton setTitle:@"确认" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = item;
    
}


- (void)layoutUI
{
    if (!_tableView) {
        _tableView = [[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[FLMTextFieldCell class] forCellReuseIdentifier:@"FLMTextFieldCell"];
        [_tableView registerClass:[FLMManageOrderDetailCell class] forCellReuseIdentifier:@"FLMManageOrderDetailCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [self addTableFooterView];
    }
    
}

- (void)addTableFooterView
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenW+60)];
    
    UIButton *imageButton = [[UIButton alloc]init];
    imageButton.backgroundColor = [UIColor lightGrayColor];
    [imageButton addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
    imageButton.tag = 9000;
    [footerView addSubview:imageButton];
    [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@30);
        make.right.equalTo(@-30);
        make.height.equalTo(imageButton.mas_width);
        make.top.equalTo(footerView.mas_top).offset(30);
    }];
    
    _imageButton = imageButton;
    
    self.tableView.tableFooterView = footerView;
}

- (void)addImage
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    //设置最大选择数量
    actionSheet.maxSelectCount = 1;
    //设置预览图最大数目
    actionSheet.maxPreviewCount = 20;
    __weak typeof(self) weakSelf = self;
    [actionSheet showWithSender:self animate:YES completion:^(NSArray<UIImage *> * _Nonnull selectPhotos) {
        if (selectPhotos.count) {
            [weakSelf.imageButton setImage:[selectPhotos firstObject] forState:UIControlStateNormal];
            _selectImage = selectPhotos.firstObject;
        }
    }];
}

- (NSMutableArray *)selectArr
{
    if (!_selectArr) {
        _selectArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _selectArr;
}

#pragma mark --UITableViewDelegate && UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        FLMTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMTextFieldCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.row ==1) {
        FLMManageOrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMManageOrderDetailCell"];
        [cell configClaimOrderWithContent:_selectStr];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
 
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==1) {
        
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for (NSString *selectStr in self.selectArr) {
            
                [sheet addButtonWithTitle:selectStr];
            }
            [sheet addButtonWithTitle:@"取消"];
        sheet.cancelButtonIndex = sheet.numberOfButtons-1;
        [sheet showInView:self.view];
        
    }
}

#pragma mark --UIActionSheet delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    _selectStr = buttonIndex>=self.selectArr.count?nil:self.selectArr[buttonIndex];
    [self.tableView reloadData];
}

#pragma mark - 私有方法

- (void)confirmAction
{
    if (!_selectStr) {
        UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选择订单类型后在提交" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
        [alertV show];
        return;
    }
    NSNumber *number_order;
    if ([_selectStr isEqualToString:@"淘宝"]) {
        number_order = [NSNumber numberWithInt:1];
    }
    else{
        number_order = [NSNumber numberWithInt:2];
    }
    FLMTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSString *string_order;
    if (!cell.textField.text.length) {
        UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"选择订单类型后在提交" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
        [alertV show];
        return;
    }
    else{
        string_order = cell.textField.text;
    }
    
    UIImage *tempImage = self.imageButton.imageView.image;
    NSData *image_data = UIImageJPEGRepresentation(tempImage, 0.5);
    NSDictionary *sendDict = @{@"id":@"1",
                               @"order_type":number_order,
                               @"order_id":@"20821939426"
                               };
    
    [[XLsn0wNetwork defaultNetwork] uploadWithURL:ClaimOrderURL token:@"" params:sendDict fileData:image_data name:@"voucher_img" fileName:@"abc.png" mimeType:@"image/png" progress:^(NSProgress *progress) {
        NSLog(@"aaaa %@",progress);
    } success:^(NSURLSessionDataTask *task, NSDictionary *JSONDictionary, NSString *JSONString) {
        if (![[JSONDictionary objectForKey:@"succ"] boolValue]) {
            UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:[JSONDictionary objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
            [alertV show];
        }
        NSLog(@"%@",JSONString);

    } failure:^(NSURLSessionDataTask *task, NSError *error, NSInteger statusCode, NSString *requestFailedReason) {
        NSLog(@"%@",requestFailedReason);
    }];
    
    
    
//    [[XLsn0wNetwork defaultNetwork] POST:ClaimOrderURL token:nil params:sendDict success:^(NSURLSessionDataTask *task, NSDictionary *JSONDictionary, NSString *JSONString) {
//        if (![[JSONDictionary objectForKey:@"succ"] boolValue]) {
//            UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:[JSONDictionary objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
//            [alertV show];
//        }
//        NSLog(@"%@",JSONString);
//        
//    } failure:^(NSURLSessionDataTask *task, NSError *error, NSInteger statusCode, NSString *requestFailedReason) {
    //        NSLog(@"%@",requestFailedReason);
//
//    }];
}

@end
